<?php
namespace app\common\util;

use gmars\qiniu\Qiniu;
use think\Cache;

class Upload
{

    /**
     * @param string $saveName
     * @param string $bucket
     * @return mixed
     * @throws Exception
     * @throws \Exception
     * 单文件上传，如果添加多个文件则只上传第一个
     */
    public static function uploadFile($savepath='',$bucketName=''){
        $upTokenTime = Cache::get('qiniu_upload_token_time');
        if(time()>$upTokenTime+3000){
            //token过期
            Cache::set('qiniu_upload_token_time', time());
            Cache::rm('qiniu_upload_token');
        }
        try{
            $accessKey = \think\Config::get('qiniu.accessKey');
            $secretKey = \think\Config::get('qiniu.secretKey');
            if(empty($bucketName)){
                $bucketName = \think\Config::get('qiniu.bucketName');
            }
            $qiniu = new Qiniu($accessKey,$secretKey,$bucketName);
            return $qiniu->upload($savepath,$bucketName);
        }catch (Exception $e){
            return false;
        }
    }


}